package com.example.muchentuner.metro;

import static android.content.Context.MODE_PRIVATE;
import static com.example.muchentuner.ButtonActivity.PREFS_FILE;
import com.example.muchentuner.metro.HomeFragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RadioGroup;

import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.example.muchentuner.R;
import com.suke.widget.SwitchButton;

public class ThirdFragment extends BaseFragment{

    private boolean Sbtn_state;
    private boolean vib_Sbtn_state;
    public static boolean Lighting;
    public static boolean Vibrate;
    private MyViewModel myViewModel;
    private static int snum = 1;

    @Override
    protected void initViews(){

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        myViewModel = new ViewModelProvider(
                requireActivity(),
                new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);

        SoundNum soundNum = new SoundNum(snum);

        sbListener();

        RadioGroup mRadioGroup =(RadioGroup) getActivity().findViewById(R.id.main_radiogroup);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                final SharedPreferences pre = getActivity().getSharedPreferences(PREFS_FILE,
                        MODE_PRIVATE);
                SharedPreferences.Editor editor = pre.edit();
                switch (checkedId){
                    case R.id.main_radiobutton1:
                        snum = 1;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton2:
                        snum = 2;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton3:
                        snum = 3;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton4:
                        snum = 4;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton5:
                        snum = 5;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton6:
                        snum = 6;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    case R.id.main_radiobutton7:
                        snum = 7;
                        soundNum.soundNum = snum;
                        myViewModel.setSoundNum(soundNum);
                        break;
                    default:
                        Log.d("TAG","怎么监听的????");
                        break;
                }
            }
        });
    }

    ImageView Lighting_image;
    ImageView Vibrate_image;
    private void sbListener() {
        com.suke.widget.SwitchButton Ref_Pitch_Button = (com.suke.widget.SwitchButton)
                getActivity().findViewById(R.id.lighting_sb);
        final SharedPreferences btn_state_pref = getActivity().getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
        Sbtn_state = btn_state_pref.getBoolean("Lighting_Sbtn_state", false);
        Ref_Pitch_Button.setChecked(Sbtn_state);//设置为真，即默认为真
        //Ref_Pitch_Button.isChecked();//被选中

        Ref_Pitch_Button.toggle();     //开关状态
        Ref_Pitch_Button.toggle(true);//开关有动画
        Ref_Pitch_Button.setShadowEffect(false);//禁用阴影效果
        Ref_Pitch_Button.setEnabled(true);//false为禁用按钮
        Ref_Pitch_Button.setEnableEffect(true);//false为禁用开关动画
        Ref_Pitch_Button.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                final SharedPreferences preferences = getActivity().getSharedPreferences(PREFS_FILE,
                        MODE_PRIVATE);
                if (Ref_Pitch_Button.isChecked()) {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("Lighting_Sbtn_state", true);//先修改REFERNECE_PITCH的键值，再调用setReferencePitch();
                    editor.apply();
                } else {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("Lighting_Sbtn_state", false);//先修改REFERNECE_PITCH的键值，再调用setReferencePitch();
                    editor.apply();
                }
                Lighting=preferences.getBoolean("Lighting_Sbtn_state",false);
                if (Lighting)
                {
                    Lighting_image=(ImageView) getActivity().findViewById(R.id.imageView_Lighting);
                    Lighting_image.setImageDrawable(getResources().getDrawable(R.drawable.ic_lighting_y));
                }
                else{
                    Lighting_image=(ImageView) getActivity().findViewById(R.id.imageView_Lighting);
                    Lighting_image.setImageDrawable(getResources().getDrawable(R.drawable.ic_lighting));
                }
            }
        });

        com.suke.widget.SwitchButton Vibrate_Button = (com.suke.widget.SwitchButton)
                getActivity().findViewById(R.id.vibrate_sb);
        final SharedPreferences btn_state_vib = getActivity().getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
        vib_Sbtn_state = btn_state_vib.getBoolean("Vibrate_Sbtn_state", false);
        Vibrate_Button.setChecked(vib_Sbtn_state);//设置为真，即默认为真
        Vibrate_Button.toggle();     //开关状态
        Vibrate_Button.toggle(true);//开关有动画
        Vibrate_Button.setShadowEffect(false);//禁用阴影效果
        Vibrate_Button.setEnabled(true);//false为禁用按钮
        Vibrate_Button.setEnableEffect(true);//false为禁用开关动画
        Vibrate_Button.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                final SharedPreferences preferences = getActivity().getSharedPreferences(PREFS_FILE,
                        MODE_PRIVATE);
                if (Vibrate_Button.isChecked()) {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("Vibrate_Sbtn_state", true);//先修改REFERNECE_PITCH的键值，再调用setReferencePitch();
                    editor.apply();
                } else {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("Vibrate_Sbtn_state", false);//先修改REFERNECE_PITCH的键值，再调用setReferencePitch();
                    editor.apply();
                }
                Vibrate=preferences.getBoolean("Vibrate_Sbtn_state",false);
                if (Vibrate)
                {
                    Vibrate_image=(ImageView) getActivity().findViewById(R.id.imageView_vibrate);
                    Vibrate_image.setImageDrawable(getResources().getDrawable(R.drawable.ic_vibrate_y));
                }
                else{
                    Vibrate_image=(ImageView) getActivity().findViewById(R.id.imageView_vibrate);
                    Vibrate_image.setImageDrawable(getResources().getDrawable(R.drawable.ic_vibrate));
                }
            }
        });

    }

    @Override
    protected int getLayoutId(){
        return R.layout.thirdfrm;
    }

}

